<template>
	<view class="detail-content mb-40">
		<view class="inform-title container">
			<image src="@/static/img/common/line.png" alt="" class="inform-title-img"></image>
			<text class="inform-title-text">预约信息</text>
		</view>
		<view class="inform-msg container">
			<view class="inform-msg-row">
				<text class="text">挂号序号</text>
				<text class="text">{{dataData.serialNo || '--'}}</text>
			</view>
			<!-- <view class="inform-msg-row">
				<text class="text">接诊专家</text>
				<text class="text">{{dataData.expert ? dataData.expert.username : '--'}}</text>
			</view> -->
			<view class="inform-msg-row">
				<text class="text">就诊人</text>
				<text class="text">{{dataData.userVisitCard.username}}</text>
			</view>
			<view class="inform-msg-row">
				<text class="text">性别</text>
				<text class="text">{{dataData.userVisitCard.sex === 0 ? '女' : '男'}}</text>
			</view>
			<view class="inform-msg-row">
				<text class="text">年龄</text>
				<text class="text">{{dataData.userVisitCard.birthday | filterAge}}</text>
			</view>
			<view class="inform-msg-row">
				<text class="text">就诊时段</text>
				<text class="text">{{ dataData.timeSlot || '--'}}</text>
			</view>
			<view class="inform-msg-row">
				<text class="text">就诊时卡号</text>
				<text class="text">{{dataData.visitCardNo || '--' }}</text>
			</view>
			<view class="inform-msg-row">
				<text class="text">院区场地</text>
				<text class="text">{{dataData.site ? dataData.site.siteName : '--'}}</text>
			</view>
		</view>
		<!-- 诊断结果 -->
		<view class="" v-if="dataData.status !== 1">
			<view class="inform-title container">
				<image src="@/static/img/common/line.png" alt="" class="inform-title-img"></image>
				<text class="inform-title-text">诊断结果</text>
			</view>
			<view class="inform-msg container">
				<view class="inform-msg-row">
					<!-- <view class="inform-msg-row-content">诊断结果</view> -->
					<text class="inform-msg-row-text">{{dataData.diagnosticContent || '--'}}</text>
				</view>
			</view>
		</view>
		<!-- 处方信息 -->
		<view class="inform-title container"  v-if="dataData.status !== 1 && dataData.prescriptionName">
			<image src="@/static/img/common/line.png" alt="" class="inform-title-img"></image>
			<text class="inform-title-text">处方信息</text>
		</view>
		<view class="inform-msg container"   v-if="dataData.status !== 1 && dataData.prescriptionName">
			<view class="inform-msg-row">
				<text class="text">处方名称</text>
				<text class="text" @tap="toDetail(dataData.expertPrescription)">{{dataData.prescriptionName || '--'}}</text>
			</view>
			<view class="inform-msg-row">
				附件
			</view>
			<view class="inform-msg-row2" v-if="fileList.length == 0">
				无
			</view>
			<view class="inform-msg-row2" v-for="(item,index) in fileList" :key="index" v-else>
				<text class="text">
					<u-icon name="file-text-fill" color="#999" size="28"></u-icon>
				</text>
				<text class="text">{{item.fileName || '--'}}.</text>
				<text class="text red" :data-type="item.fileName | getTypeName" :data-url="item.url" :data-img-url="item.url" @tap="previewAll">预览</text>
				<text class="red" @click="downloadFile(item.url)">下载</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { API } from "@/config/myApi.js";
	import {filterAge} from "@/utils/filters.js";
	export default {
		data() {
			return {
				dataData:[],
				fileList: []
			};
		},
		onLoad() {
			this.dataData =	uni.getStorageSync("expertItemDetail");
			console.log('dataData',this.dataData);
			if(this.dataData.status !== 1){
				API.prescriptionDetails(this.dataData.id).then(res => {
					if (res.code === 200) {
						this.fileList = res.result;
						console.log('附件',this.fileList);
					} else {
						uni.showToast({
							title: res.message,
							icon: "none"
						})
					}
				})
				// console.log('详情',this.dataData);
			}
		},
		methods: {
			// 去处方详情
			toDetail(item) {
				uni.setStorageSync('expertItemDetail',item)
				uni.navigateTo({
					url:"/pages/mine/myConsultation/prescription/detail?isShow=false"
				})
			},
			//文件预览
			previewFile(url,typeName) {
				// console.log('e =====> 预览',url,'typeName',typeName);
				//支持预览的文件类型
				//微信小程序
				let fileType = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx' ]; 
				// if(!fileType.includes(typeName)) {
				//   return uni.showToast({
				//     title: '不支持预览当前文件类型',
				//     icon: 'none'
				//   })
				// }
				//下载文件资源到本地
				uni.downloadFile({
					url,
					header:{'content-type':"content-disposition"},
					success: (res) => {
						if (res.statusCode === 200) {
							console.log('下载成功', res);
							let filePath = res.tempFilePath;
							// if(!fileType.includes(typeName)) {
							// 	return false;
							// }
							uni.showLoading({
								title: '正在打开',
								mask: true
							})
							uni.openDocument({
								filePath: res.tempFilePath,
								fileType: url.substr(1, url.length), // 指定文件的格式
								// showMenu: true, // 允许出现分享功能
								success: r => {
									console.log('打开文档成功 ===> res',r)
								},
								fail: openError => {
									console.log('打开失败: ', openError)
								}
							})
							uni.hideLoading();
						}
					},
					fail() {
						uni.showToast({
							title: '预览失败',
							icon:"none"
						})
					}
				})
			},
			//预览图片
			previewImg(imgUrl) {
				console.log('imgUrl',imgUrl);
				// 预览图片
				uni.previewImage({
					urls: [ imgUrl ],
					longPressActions: {
						// itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							//console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			previewAll({ currentTarget: { dataset: { url, imgUrl, type } } }) {
				// console.log('类型',type);
				// el.type === 'png' || el.type === 'jpg' || el.type === 'jpeg'
				let fileType = ['png', 'jpg', 'jpeg','PNG','JPEG','JPG']; // 图片类型
				console.log('fileType.includes(type)',fileType.includes(type));
				if(fileType.includes(type)) {
					this.previewImg(imgUrl);
				} else {
					this.previewFile(url,type);
				}
			},
			downloadFile(val) {
				// let url = encodeURI(val);
				let url =val;
				console.log('url',url);
				uni.showLoading({
					title: '下载中',
					mask: true
				}),
				uni.downloadFile({
					url,
					success: (res) => {
						if (res.statusCode === 200) {
							console.log('下载成功', res);
							uni.hideLoading();
							uni.openDocument({
								filePath: res.tempFilePath,
								fileType: url.substr(1, url.length), // 指定文件的格式
							})
						}
					},
					fail() {
						// console.log('下载失败');
						uni.showToast({
							title: '下载失败',
						})
					}
				})
			}
		},
		filters:{
			getTypeName(val){
				// 上传文件类型校验
				let imgType = ['png', 'jpg', 'jpeg','PNG','JPEG','JPG']; // 图片类型
				let fileType = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx' ]; // 其他类型
				let typeName = imgType.filter(v =>{
					if(val.includes(v)){
						console.log(v,'v');
						return v
					}
				})
				if(typeName.length === 0){
					typeName = imgType.filter(v =>{
						if(val.includes(v)){
							console.log(v,'pdf');
							return v
						}
					})
				}
				return typeName[0];
			}
		}
	}
</script>

<style lang="scss">
	@import "./registerDetail.scss"
</style>
